package willevaluate.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;

import java.util.ArrayList;

import willevaluate.model.QuestionnaireProvModel;

/**
 * Created by Administrator on 2017/6/8.
 */

public class QuestionnaireProvAdapter extends BaseAdapter {
    private ArrayList<QuestionnaireProvModel> arrayList;
    private Context context;
    public static final int TYPE_GROUP = 0;
    public static final int TYPE_ITEM = 1;


    public QuestionnaireProvAdapter(ArrayList<QuestionnaireProvModel> arrayList, Context context) {
        this.arrayList = arrayList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getItemViewType(int position) {
        return arrayList.get(position).getType();
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public boolean isEnabled(int position) {
        if (arrayList.get(position).getType() == TYPE_GROUP) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        switch (getItemViewType(position)) {
            case TYPE_GROUP:
                GroupHolder groupHolder;
                if (convertView == null) {
                    groupHolder = new GroupHolder();
                    convertView = LayoutInflater.from(context).inflate(R.layout.questionnaire_prov_group, null);
                    groupHolder.tv = (TextView) convertView.findViewById(R.id.questionnaire_prov_group_tv);
                    convertView.setTag(groupHolder);
                } else {
                    groupHolder = (GroupHolder) convertView.getTag();
                }
                groupHolder.tv.setText(arrayList.get(position).getPro_name());
                break;
            case TYPE_ITEM:
            default:
                ItemHolder itemHolder;
                if (convertView == null) {
                    itemHolder = new ItemHolder();
                    convertView = LayoutInflater.from(context).inflate(R.layout.questionnaire_prov_item, null);
                    itemHolder.name = (TextView) convertView.findViewById(R.id.questionnaire_prov_item_name);
                    itemHolder.checked = (ImageView) convertView.findViewById(R.id.questionnaire_prov_item_checked);
                    convertView.setTag(itemHolder);
                } else {
                    itemHolder = (ItemHolder) convertView.getTag();
                }
                itemHolder.name.setText(arrayList.get(position).getPro_name());
                if (arrayList.get(position).isChecked()) {
                    itemHolder.checked.setImageResource(R.mipmap.college_evaluate_checked_ima);
                } else {
                    itemHolder.checked.setImageResource(R.mipmap.college_evaluate_check_ima);
                }
                break;
        }
        return convertView;
    }


    class GroupHolder {
        TextView tv;
    }

    class ItemHolder {
        TextView name;
        ImageView checked;
    }
}
